//
//  WordVC.m
//  sweetbox
//
//  Created by apollo on 15/9/8.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "WordVC.h"
#import "DBHelper.h"
#import "log.h"

@interface WordVC ()
@property BOOL modifialbe;
@end

@implementation WordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"word is %s",[self.word cStringUsingEncoding:NSUTF8StringEncoding]);
    
    NSArray *word = [[DBHelper sharedDBHelper] wordAtKey:self.word];
    self.wordTF.text = word[0];
    self.translateTF.text = word[1];
    self.wordTF.enabled = NO;
    self.modifialbe = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onModify:(id)sender {
    NSString *word = self.wordTF.text;
    NSString *translate = self.translateTF.text;
    
    if([word isEqualToString:@""] || [translate isEqualToString:@""]) {
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完成" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return ;
    }
    NSLog(@"word :%@",word);
    NSLog(@"translate :%@",translate);
    
    BOOL ret = [[DBHelper sharedDBHelper] updateWord:word translate:translate];
    if (YES == ret) {
        LOG_I("Update word %s Success",[word cStringUsingEncoding:NSUTF8StringEncoding] );
        
    } else {
        LOG_E("Update word %s Failed",[word cStringUsingEncoding:NSUTF8StringEncoding] );
    }
    UINavigationController *vc = (UINavigationController *)  self.parentViewController;
    [vc popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"save_word" object:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)onTranlateDone:(id)sender {
    [sender resignFirstResponder];
}

@end
